package cloud.tianai.ram.policy.server.resource.impl;

import cloud.tianai.ram.policy.common.model.dto.Statement;
import cloud.tianai.ram.policy.common.model.dto.Action;
import cloud.tianai.ram.policy.common.model.dto.Match;
import cloud.tianai.ram.policy.common.handler.ResourceHandler;
import org.springframework.core.annotation.Order;
import org.springframework.util.AntPathMatcher;

import java.util.Collection;

/**
 * @Author: 天爱有情
 * @date 2022/3/7 14:34
 * @Description ant resource handler
 */
@Order(10)
public class AntResourceHandler implements ResourceHandler {

    private AntPathMatcher antPathMatcher = new AntPathMatcher();


    @Override
    public Match match(String currentResource, String matchResource) {
        if (antPathMatcher.match(matchResource, currentResource)) {
            return Match.ALLOW;
        }
        return Match.IMPLICIT_DENY;
    }

    @Override
    public Match match(String currentResource, Collection<String> matchResource) {
        for (String resource : matchResource) {
            Match match = match(currentResource, resource);
            if (match.isAllow()) {
                return match;
            }
        }
        return Match.IMPLICIT_DENY;
    }

    @Override
    public Match handle(Action action, Statement statement) {
        return match(action.getResource(), statement.getResourceList());
    }


    public static void main(String[] args) {
        AntPathMatcher antPathMatcher = new AntPathMatcher();
    }
}
